# @Time    :2025/8/7 下午2:56
# @Author  :XiaoJiang
# @File    :反射案例.py
# @Software: PyCharm
import sys


# Info: 模拟购物商城  t_menu
class Manager:
    role_menu = [
        ("【系统管理】","systemManager"),
        ("【权限管理】","roleManager"),
        ("【用户管理】","userManager"),

    ]
    def __init__(self,name):
        self.name = name

    def systemManager(self):
        print(self.name + "执行了系统管理功能...")

    def roleManager(self):
        print(self.name + "执行了权限管理功能...")

    def userManager(self):
        print(self.name + "执行了用户管理功能...")

class Seller:
    role_menu = [
        ("【商品上架】", "goodsAdd"),
        ("【商品下架】", "goodsDelete"),
        ("【商品发货】", "goodsSend"),

    ]

    def __init__(self, name):
        self.name = name

    def goodsAdd(self):
        print(self.name + "执行了商品上架功能...")

    def goodsDelete(self):
        print(self.name + "执行了商品下架功能...")

    def goodsSend(self):
        print(self.name + "执行了商品发货功能...")

class Buyer:
    role_menu = [
        ("【商品查看】", "goodsInfo"),
        ("【商品下单】", "goodsOrder"),
        ("【商品收获】", "goodsRecv"),

    ]

    def __init__(self, name):
        self.name = name
        self.role_menu = Buyer.role_menu
        print(self.name + "执行了商品查看功能...")

    def goodsInfo(self):
        print(self.name + "执行了商品查看功能...")

    def goodsOrder(self):
        print(self.name + "执行了商品下单功能...")

    def goodsRecv(self):
        print(self.name + "执行了商品收获功能...")

# 统一的登录入口
def login():
    username = input("请输入用户名：")
    password = input("请输入密码：")

    with open("userrole") as file:
        for line in file:
            username1, password1,role = line.strip().split(",")
            if username == username1 and password == password1:
                print("登录成功")
                return username1,role


def main():
    user,role = login()

    # 获取当前模块
    myModule = sys.modules[__name__]
    # 获取类本身
    cls = getattr(myModule,role)
    # 反射 类创建对象
    obj = cls(user)
    # 获取类属性
    role_menu = cls.role_menu

    while 1:
        for i,menu in enumerate(role_menu,1):
            print(i,menu[0])

        num = int(input("请选择功能："))

        menu_name = role_menu[num-1][1]

        # print(menu_name)

        # 通过反射执行方法
        getattr(obj,menu_name)()

main()

